<div id="admin_container">
<h1>Historial de cambios a "<?php echo $auditoria_entidad->getEntidad() ?>"</h1>

  <?php include_partial('auditoria_entidad/flashes') ?>

  <div id="admin_content">

  <form action="<?php echo url_for('auditoria_entidad_collection', array('action' => 'batchVersion')), '?id=', $auditoria_entidad->getId() ?>" method="post">
  <ul class="admin_actions">
  <li class="admin_batch_actions_choice">
    <select name="batch_action">
      <option value="">Seleccione una acción</option>
      <option value="batchDeleteVersion">Borrar</option>
    </select>
    <?php $form = new BaseForm(); if ($form->isCSRFProtected()): ?>
      <input type="hidden" name="<?php echo $form->getCSRFFieldName() ?>" value="<?php echo $form->getCSRFToken() ?>" />
    <?php endif; ?>
    <input type="submit" value="ok" />
  </li>
  </ul>
  <div class="admin_list">
    <?php if ($pager->getNbResults() == 0): ?>
      <p>Sin resultados</p>
    <?php else: ?>
      <table>
      <thead>
        <tr>
          <th id="admin_list_batch_actions">
            <input id="admin_list_batch_checkbox" type="checkbox" onclick="checkAll();">
          </th>
          <th>Id Entidad</th>
          <th>Versión</th>
          <th>Usuario</th>
          <th>Fecha</th>
          <th id="admin_list_th_actions">Acciones</th>
        </tr>
      </thead>
      <tfoot>
        <tr>
          <th colspan="6">
            <?php if ($pager->haveToPaginate()) : ?>
              <?php include_partial('auditoria_entidad/historial_pagination', array('pager' => $pager, 'id' => $auditoria_entidad->getId())) ?>
            <?php endif; ?>
            <?php echo $pager->getNbResults(), ' ', $pager->getNbResults() == 1 ? 'resultado' : 'resultados' ?>
            <?php if ($pager->haveToPaginate()): ?>
              <?php printf('(página %s/%s)', $pager->getPage(), $pager->getLastPage()) ?>
            <?php endif; ?>
          </th>
        </tr>
      </tfoot>
      <tbody>
      <?php foreach($pager->getResults(Doctrine_Core::HYDRATE_ARRAY) as $version): ?>
        <tr> 
          <td><input class="admin_batch_checkbox" type="checkbox" value="<?php echo($version['version'])?>" name="versiones[]"></td>
          <td class="admin_text"><?php echo $version['id'] ?></td>
          <td class="admin_text"><?php echo $version['version'] ?></td>
          <td class="admin_text"><?php echo $version['sfGuardUser']['username'] ?></td>
          <td class="admin_text"><?php echo $version['fecha'] ?></td>
          <td>
            <a onClick="compareVersion(<?php echo $version['id'] . ', ' .$version['version'] ?>)"><img class="admin_td_action" src="/images/default.png" alt="Comparar" title="Comparar con versión actual"></img></a>
            <a onClick="deleteVersion(this, <?php echo $version['version'] ?>)"><img class="admin_td_action" src="/images/delete.png" alt="Eliminar" title="Eliminar"></img></a>
          </td>
        </tr>
      <?php endforeach; ?>
      </tbody>
      </table>
    <?php endif; ?>
  </div>

  <script type="text/javascript">
  /* <![CDATA[ */
  function checkAll()
  {  
    var boxes = document.getElementsByTagName('input');
    for(var index = 0; index < boxes.length; index++) { 
      box = boxes[index];
      if (box.type == 'checkbox' && box.className == 'admin_batch_checkbox') 
        box.checked = document.getElementById('admin_list_batch_checkbox').checked 
    }
    return true;
  }
  
  function compareVersion(id, version)
  {
    var f = document.createElement('form'); 
    f.style.display = 'none';
    f.method = 'post';
    f.action = '<?php echo url_for('@auditoria_entidad_comparar?id=' . $auditoria_entidad->getId()) ?>';
    document.getElementById('admin_content').appendChild(f);
    
    var m = document.createElement('input');
    m.setAttribute('type', 'hidden');
    m.setAttribute('name', 'version');
    m.setAttribute('value', version); 
    f.appendChild(m);   
    
    m = document.createElement('input');
    m.setAttribute('type', 'hidden');
    m.setAttribute('name', 'entidad_id');
    m.setAttribute('value', id);
    f.appendChild(m);
    
    <?php if ($form->isCSRFProtected()) : ?>      
    m = document.createElement('input');
    m.setAttribute('type', 'hidden'); 
    m.setAttribute('name', '<?php echo $form->getCSRFFieldName()?>');
    m.setAttribute('value', '<?php echo $form->getCSRFToken()?>'); 
    f.appendChild(m);
    <?php endif; ?>
            
    f.submit();
  }
  
  function deleteVersion(node, version)
  {       
    if (confirm('¿Desea eliminar la versión seleccionada?'))
    { 
      var f = document.createElement('form'); 
      f.style.display = 'none';
      f.method = 'post';
      f.action = '<?php echo url_for('@auditoria_entidad_eliminarVersion?id=' . $auditoria_entidad->getId()) ?>';
      document.getElementById('admin_content').appendChild(f);
      
      var m = document.createElement('input');
      m.setAttribute('type', 'hidden');
      m.setAttribute('name', 'sf_method');
      m.setAttribute('value', 'delete');
      f.appendChild(m);
      
      m = document.createElement('input');
      m.setAttribute('type', 'hidden');
      m.setAttribute('name', 'version');
      m.setAttribute('value', version);
      f.appendChild(m);
      
      <?php if ($form->isCSRFProtected()) : ?>      
      m = document.createElement('input');
      m.setAttribute('type', 'hidden'); 
      m.setAttribute('name', '<?php echo $form->getCSRFFieldName()?>');
      m.setAttribute('value', '<?php echo $form->getCSRFToken()?>'); 
      f.appendChild(m);
      <?php endif; ?>
            
      f.submit();
    };
    return false;
  }
  
  /* ]]> */
  </script>

  </form>
  </div>
</div>
